package com.chance.cc.crawler.development.bootstrap.cnhubei;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.controller.DevCrawlerController;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRecordFilter.*;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/3/10 13:49
 * @Description
 *      东湖社区 启动类
 **/
public class CnHubei {
    private static final String domain = "cnhubei";
    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("HEW657EL99F83S9D");
        proxy.setPassword("8916B1F3F10B1979");
    }


    public static void main(String[] args) {
//        itemRecord();
        keywordRecord();
    }

    private static void keywordRecord(){
        String url = "http://bbs.cnhubei.com/search.php?mod=forum";
        String site = "searchKw";
        String siteBiz = "forum";
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPageItem)
                .domain(domain)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 30,null))
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .build();
        crawlerRequestRecord.setDownload(false);
        crawlerRequestRecord.setSkipPipeline(true);
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);
        Map<String,String> heads = new HashMap<>();
        heads.put("Content-Type","application/x-www-form-urlencoded");
        heads.put("Host","bbs.cnhubei.com");
        heads.put("Origin","http://bbs.cnhubei.com");
        heads.put("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36");
        heads.put("Cookie","haoe_af79_saltkey=CxAl2595; haoe_af79_lastvisit=1615351251; haoe_af79_pc_size_c=0; wdcid=6fedd44fca9c92f0; Hm_lvt_7c7368145650bd2ffeefcc96d2006f63=1615354863,1615431459; Hm_lvt_941d8bc973cf2e92916773f2b97fb56b=1615358144,1615434682; haoe_af79_sid=VUqI9P; Hm_lpvt_941d8bc973cf2e92916773f2b97fb56b=1615439920; wdlast=1615439921; wdses=2d814b87884ba544");
        heads.put("Referer","http://bbs.cnhubei.com/search.php?mod=forum");
        crawlerRequestRecord.getHttpRequest().setHeaders(heads);

        CrawlerRecord commentFilter = new CrawlerRequestRecord();
        commentFilter.setFilter(key);
        commentFilter.addFilterInfo(FilterUtils.memoryFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,"queue")));
//        commentFilter.addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 1,null));
        crawlerRequestRecord.tagsCreator().resultTags().getCategoryTag().addKVTag("comment_filter_record", JSON.toJSONString(commentFilter));
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("searchKwSourceUrl","http://bbs.cnhubei.com/search.php?mod=forum&searchid=%s&orderby=dateline&ascdesc=desc&searchsubmit=yes&kw=%s");
        Map<String,Object> map = new HashMap<>();
//        map.put("formhash","0c0f9112");
        map.put("searchsubmit","yes");
        map.put("srchtxt","%s");
        Map<String,Object> extras = new HashMap<>();
        extras.put("httpBody",map);
        crawlerRequestRecord.getHttpRequest().setExtras(extras);

        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("cnhubei_forum_keyword",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/v1/meta/"+domain+"/keys?site="+siteBiz)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        DevCrawlerController.builder()
                .triggerInfo(domain,domain,System.currentTimeMillis(),domain)
                .crawlerRequestQueue(DevCrawlerController.devRequestQueue(domain))
                .consoleResultPipeline()//控制台输出
//                .fileResultPipeline("D:\\chance\\log\\tets.log",true) //文件输出
                .requestRecord(crawlerRequestRecord)
                .supportRecord(keywordRecord)
                .build()
                .start();
    }

    private static void itemRecord(){
        String url = "http://bbs.cnhubei.com/forum.php?mod=viewthread&tid=4789565&highlight=%E4%B8%A4%E4%BC%9A";
        String site = "forum";
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPageItem)
                .domain(domain)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(domain))
//                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
//                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 20,null))
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .build();

        CrawlerRecord commentFilter = new CrawlerRequestRecord();
        commentFilter.setFilter(key);
//        commentFilter.addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 1,null));
        commentFilter.addFilterInfo(FilterUtils.memoryFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,"queue")));
        crawlerRequestRecord.tagsCreator().resultTags().getCategoryTag().addKVTag("comment_filter_record", JSON.toJSONString(commentFilter));

        DevCrawlerController.builder()
                .triggerInfo(domain,domain,System.currentTimeMillis(),domain)
                .crawlerRequestQueue(DevCrawlerController.devRequestQueue(domain))
                .consoleResultPipeline()//控制台输出
//                .fileResultPipeline("D:\\chance\\log\\tets.log",true) //文件输出
                .requestRecord(crawlerRequestRecord)
                .build()
                .start();
    }

}
